package com.juanLL.CMC.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.SoundPool;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.juanLL.CMC.GameLoop;
import com.juanLL.CMC.controller.InputController;
import com.juanLL.CMC.models.Citizen;
import com.juanLL.CMC.models.GameWorld;
import com.juanLL.CMC.models.Rat;
import com.juanLL.CMC.models.RecycleBin;
import com.juanLL.CMC.models.Trash;

/**
 * GameView class handles the rendering of the game state to the canvas
 * 
 */
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
	private int mSoundID;
	private SoundPool mSound;
	boolean loaded = false;

	// log tag constant id for debugging
	public static String TAG = "GameView";
	// GameWorld object that contains the game state to be drawed
	private GameWorld gameWorld;
	// GameLoop object that runs the main loop
	public GameLoop gameLoop;
	// Rect that contains the screen resolution
	private Rect screenRes;
	// paint for line drawing
	private Paint p = new Paint();
	// Model objects
	private Trash t;
	private RecycleBin r;
	private Citizen cit;
	private Rat rat;

	/**
	 * GameView constructor
	 * 
	 * @param context
	 */
	public GameView(Context context) {
		super(context);
		// add the surfaceHolder callback to this object
		getHolder().addCallback(this);
		// get the screen resolution
		getScreenRes(context);
		// create a new GameWorld object
		this.gameWorld = new GameWorld(context, screenRes);
		// create a new GameLoop object
		this.gameLoop = new GameLoop(this, gameWorld);
		new InputController(gameWorld, this);

		p.setTextSize(60);
		p.setColor(0xFFFFFFFF);

	}

	/**
	 * setScreenRes, measures the screen's resolution and stores the info on a
	 * Rect object.
	 */

	@SuppressWarnings("deprecation")
	private void getScreenRes(Context context) {
		int w;
		int h;
		// create a window manager that contains the display
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		// get current display's width
		w = wm.getDefaultDisplay().getWidth();
		// get current display's height
		h = wm.getDefaultDisplay().getHeight();
		// create the RectF object with the screen resolution
		screenRes = new Rect(0, 0, w, h);
	}

	/**
	 * onDraw method, handles drawing to the canvas
	 * 
	 * @param c
	 *            the canvas to draw on
	 */
	@Override
	public void onDraw(Canvas c) {

		if (!gameWorld.transition && !gameWorld.end) {
			// draw the background
			c.drawBitmap(gameWorld.background, 0, 0, null);

			// draw the pause button
			c.drawBitmap(gameWorld.pauseBitmap, gameWorld.pausex,
					gameWorld.pausey, null);

			// draw the bins
			for (int i = 0; i < gameWorld.recycleBinList.size(); i++) {
				r = gameWorld.recycleBinList.get(i);
				c.drawBitmap(r.bitmap, r.x, r.y, null);
			}

			// draw the citizens
			for (int i = 0; i < gameWorld.citizenList.size(); i++) {
				cit = gameWorld.citizenList.get(i);
				if (cit.draw) {
					c.drawBitmap(cit.bitmap, cit.x, cit.y, null);
				}
			}

			// draw the rat
			if (gameWorld.rat.isSpawned) {
				rat = gameWorld.rat;
				c.drawBitmap(rat.bitmap, rat.x, rat.y, null);
			}

			// draw the trash
			if (!gameWorld.trashList.isEmpty()) {
				for (int i = 0; i < gameWorld.trashList.size(); i++) {
					t = gameWorld.trashList.get(i);
					if (t.draw) {
						c.drawBitmap(t.bitmap, t.x, t.y, null);

					}

				}
			}

			// draw the score
			if (gameWorld != null) {
				c.drawText("Score: " + gameWorld.score + " /"
						+ gameWorld.winScore, 10, 40, p);
			}

			// draw the pause dialog
			if (gameWorld.pause && !gameWorld.end) {
				c.drawText("GAME PAUSED", screenRes.centerX(),
						screenRes.centerY(), p);
			}

			// scale the canvas to fit the screen
			c.scale(screenRes.right, screenRes.bottom);

		} else {
			// draw the splash screen
			c.drawBitmap(gameWorld.splashScreen, 0, 0, null);

			// scale the canvas to fit the screen
			// c.scale(screenRes.right, screenRes.bottom);
		}
	}

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	public void surfaceCreated(SurfaceHolder arg0) {
		// if the game loop isn't alive
		if (!gameLoop.isAlive()) {
			// create a new game loop
			gameLoop = new GameLoop(this, gameWorld);
		}

		// set the boolean controller of the loop to true
		gameLoop.setRunning(true);
		// start the game loop
		gameLoop.start();
	}

	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		gameLoop.setRunning(false);
	}

}
